uniform sampler2D tex_pp, tex_depth, tex_norm, tex_motion, tex_ssao_rnd;
uniform mat4 invprojmtx, projmtx, modelviewmtx, invmodelviewmtx;

float linear_depth(float d) 
{ 
    return -projmtx[3].z/((d * -2.0) + 1.0 - projmtx[2].z);
} 

float calc_occlusion(float origo_depth, vec3 rnd_dir, vec3 point, float radius)
{
	vec3 rnd_coord = radius * point * rnd_dir.r;//reflect(point, rnd_dir);
	float point_depth = texture2D(tex_depth, gl_TexCoord[0].st + rnd_coord.xy).r;
	float linear_depth = linear_depth(point_depth) + rnd_coord.z;

	float dd = max(origo_depth - linear_depth, 0.0);

	if(dd > 1.5) 
        return 1.0; 
 
    return 1.0 / (1.0 + dd * dd * 100.0); 

}

float SSAO(float sample_radius)
{
	int sample_count = 8;
	float sample_count_f = 8.0;

	float depth = texture2D(tex_depth, gl_TexCoord[0].st).r;
	float pixel_depth = linear_depth(depth);

	float accum = 0.0;

	vec3 smpl_pos[8];
	smpl_pos[0] = normalize(vec3( 1.0,  1.0,  1.0)); 
	smpl_pos[1] = normalize(vec3( 1.0,  1.0, -1.0)); 
	smpl_pos[2] = normalize(vec3( 1.0, -1.0,  1.0)); 
	smpl_pos[3] = normalize(vec3( 1.0, -1.0, -1.0)); 
	smpl_pos[4] = normalize(vec3(-1.0,  1.0,  1.0)); 
	smpl_pos[5] = normalize(vec3(-1.0,  1.0, -1.0)); 
	smpl_pos[6] = normalize(vec3(-1.0, -1.0,  1.0)); 
	smpl_pos[7] = normalize(vec3(-1.0, -1.0, -1.0)); 

	float offset = 0.0;

	for (int i = 0; i < sample_count; i++)
	{
		offset = texture2D(tex_ssao_rnd, (gl_TexCoord[0].st+offset)).r;
		if (i < 8)
			accum += calc_occlusion(pixel_depth, offset, smpl_pos[i], sample_radius);
		else
			accum += calc_occlusion(pixel_depth, offset, -smpl_pos[8-i], sample_radius);
	}

	
	return accum / sample_count_f;
}
void main (void)
{
	//vec4 t_out0 = texture2D(tex_pp, gl_TexCoord[0].st + vec2(0.0, -0.001));
	//vec4 t_out1 = texture2D(tex_pp, gl_TexCoord[0].st + vec2(0.0, 0.001));
	//vec4 t_out2 = texture2D(tex_pp, gl_TexCoord[0].st + vec2(-0.001, 0.0));
	//vec4 t_out3 = texture2D(tex_pp, gl_TexCoord[0].st + vec2(0.001, 0.0));
	//gl_FragColor = (t_out0 + t_out1 + t_out2 + t_out3) / 4.0;

	//vec4 t_out0 = texture2D(tex_pp, gl_TexCoord[0].st);
	
	vec4 t_out0 = texture2D(tex_pp, gl_TexCoord[0].st);
	//vec4 t_out1 = texture2D(tex_ssao_rnd, gl_TexCoord[0].st * 2.0);
	float t_test = SSAO(0.03);
	//if (gl_TexCoord[0].s > 0.5)
		gl_FragColor = t_test;//t_out0 * t_test;//t_out0;
	//else
	//	gl_FragColor = t_test;//t_out0;

	//gl_FragColor = t_out0 * t_test;

	//gl_FragColor = t_out1;
	//gl_FragColor = t_out0;

	// blur

	/*vec2 ScaleU = vec2(0.003, 0.003);
	vec4 color = vec4(0.0);

	color += texture2D( tex_pp, gl_TexCoord[0].st + vec2( -3.0*ScaleU.x, -3.0*ScaleU.y ) ) * 0.015625;
	color += texture2D( tex_pp, gl_TexCoord[0].st + vec2( -2.0*ScaleU.x, -2.0*ScaleU.y ) ) *0.09375;
	color += texture2D( tex_pp, gl_TexCoord[0].st + vec2( -1.0*ScaleU.x, -1.0*ScaleU.y ) ) *0.234375;
	color += texture2D( tex_pp, gl_TexCoord[0].st + vec2( 0.0 , 0.0) ) * 0.3125;
	color += texture2D( tex_pp, gl_TexCoord[0].st + vec2( 1.0*ScaleU.x,  1.0*ScaleU.y ) ) *0.234375;
	color += texture2D( tex_pp, gl_TexCoord[0].st + vec2( 2.0*ScaleU.x,  2.0*ScaleU.y ) ) *0.09375;
	color += texture2D( tex_pp, gl_TexCoord[0].st + vec2( 3.0*ScaleU.x, -3.0*ScaleU.y ) ) * 0.015625;

	gl_FragColor = color;*/
}
